#!/usr/bin/env python
# modified from http://www.larsen-b.com/Article/204.html

import sys
try:
    from elementtree import ElementTree
except:
    from xml.etree import ElementTree

try:
   file = sys.argv[1]
   data = open(file,'r').read()
except:
   print sys.argv[0] + " {kismet logfile}"
   sys.exit(1)


detection = ElementTree.XML(data)

print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example: Simple Markers</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAseq-gyucA3L3mH2nWByW5hSb3p1oDoSWyJ5IzvZjwSJ-WeVekBSC6NevBmfOWzudzEAqEeqBu2bntw"
            type="text/javascript"></script>
    <script type="text/javascript">

    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(46.58906908309182, 0.340576171875), 13);
	map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());


        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);

            function createMarker(point,SSID, channel) {
                  var letteredIcon = new GIcon(baseIcon);
                  letteredIcon.image = "http://www.google.com/mapfiles/markerA.png";

                  markerOptions = { icon:letteredIcon };
                  var marker = new GMarker(point, markerOptions);

                  GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml("SSID : " + SSID + "<br />Canal : " + channel );
                  });
                  return marker;
                }
"""

for node in detection.getchildren():
   try:
      ssid = node.find('SSID').text
   except AttributeError:
      #hidden SSID
      ssid = "{unknown SSID}"

   bssid = node.find('BSSID').text
   ssid = ssid.replace('&','')
   channel = node.find('channel').text
   maxrate = node.find('maxrate').text 
   encryption = node.find('encryption').text

   gps = node.find('gps-info')
   lon = gps.find('max-lon').text
   lat = gps.find('max-lat').text

   print "		map.addOverlay(createMarker(new GLatLng(%s, %s), \"%s\", %s));" % (lat, lon, ssid,channel)


# KML Footer
print """
		}
	}
    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 1024px; height: 768px"></div>
  </body>
</html>
</div></body></script></script></title></head></html>
"""
